package dart

class PlayerGame {
	List rounds = new ArrayList()
	Integer status = IN_PROGRESS;
	Person person;

	static belongsTo = [game: Game]
	static hasMany = [rounds: Round]
	static Integer IN_PROGRESS = 0
	static Integer LOST = 1
	static Integer TIE = 2
	static Integer WON = 3
	
	public newRound = {List attempts ->
		Round round = new Round(playerGame: this);
		Float startingScore = currentScore();
		Boolean done = false;
		Float score = startingScore;

		attempts.each {Attempt attempt ->
			if (done)
				return

			round.attempts << attempt;
			attempt.harshPenalty = game.harshPenalty(score);
			attempt.round = round;
			score = round.getScore(startingScore)
			if( round.isBust(startingScore) ){
				score = startingScore;
				done = true;
			} else if ( game.isWinningScore(score) && !game.forceAllThrows)
				done = true;
		}
		rounds << round;
		return score;
	}

	public Float currentScore() {
		Float score = game.startingScore;
		rounds.each {Round round ->
			score = round.getScore(score);
		}
		return score;
	}
}